package com.pms.businesslogic;

import com.pms.platform.dao.UserDao;
import com.pms.platform.dto.User;
import com.pms.utils.ValueObject;

public class UserBusinessLogic implements BusinessDelegator {
	public ValueObject create( ValueObject inValObj){
		ValueObject resultObj=new ValueObject();
		try{
			User _user=new User();
			long studioId=Long.parseLong(inValObj.get("studioId").toString().trim());
			String login =inValObj.get("login").toString().trim();

			User existUserWIthLogin=UserDao.getInstance().findWhereLoginIdEqualsForStudio(studioId,login);
			if(existUserWIthLogin==null){
				_user.setName(inValObj.get("name").toString().trim());
				_user.setLoginId(inValObj.get("login").toString().trim());
				_user.setPassword(inValObj.get("password").toString().trim());
				_user.setAddress(inValObj.get("address").toString().trim());
				_user.setTitle(inValObj.get("title").toString().trim());
				_user.setMobileNo(inValObj.get("mobile").toString().trim());
				_user.setEmailId(inValObj.get("emailId").toString().trim());
				_user.setZipCode(inValObj.get("pincode").toString().trim());
				_user.setStateId(Long.parseLong(inValObj.get("stateId").toString().trim()));
				_user.setCityId(Long.parseLong(inValObj.get("cityId").toString().trim()));
				_user.setCountryId(Long.parseLong(inValObj.get("countryId").toString().trim()));
				_user.setStatus(inValObj.get("status").toString().trim());
				_user.setDeleted(false);

				UserDao.getInstance().insert(_user);
			}else{
				resultObj.put("error", "Same Login is already used for other User. Please change Login.");
				System.out.println("@@@ Same Login is already used for other User. Please change Login.");
			}
		}catch (Exception e) {
			e.printStackTrace();
		}


		return resultObj;
	}
	public ValueObject delete( ValueObject inValObj){
		ValueObject resultObj=new ValueObject();
		try{
			long userId=Long.parseLong(inValObj.get("selectedData").toString().trim());
			UserDao.getInstance().delete(userId);
		}catch (Exception e) {
		}
		return resultObj;
	}
	public ValueObject updateData( ValueObject inValObj){
		ValueObject resultObj=new ValueObject();
		try{
			long userId=Long.parseLong(inValObj.get("selectedData").toString().trim());

			System.out.println("@@@@@@@@@@ updating userId"+ userId);
			long studioId=Long.parseLong(inValObj.get("studioId").toString().trim());
			String login =inValObj.get("login").toString().trim();

			User existUserWIthLogin=UserDao.getInstance().findWhereLoginIdEqualsForStudio(studioId,login);
			if(existUserWIthLogin==null){

				User _user=UserDao.getInstance().findByPrimaryKey(userId);

				//_user.setStudioId());
				_user.setName(inValObj.get("name").toString().trim());
				_user.setLoginId(inValObj.get("login").toString().trim());
				_user.setPassword(inValObj.get("password").toString().trim());
				_user.setAddress(inValObj.get("address").toString().trim());
				_user.setTitle(inValObj.get("title").toString().trim());
				_user.setMobileNo(inValObj.get("mobile").toString().trim());
				_user.setEmailId(inValObj.get("emailId").toString().trim());
				_user.setZipCode(inValObj.get("pincode").toString().trim());
				_user.setStateId(Long.parseLong(inValObj.get("stateId").toString().trim()));
				_user.setCityId(Long.parseLong(inValObj.get("cityId").toString().trim()));
				_user.setCountryId(Long.parseLong(inValObj.get("countryId").toString().trim()));
				_user.setStatus(inValObj.get("status").toString().trim());
				_user.setDeleted(false);

				UserDao.getInstance().update(userId, _user);
			}else{
				resultObj.put("error", "Same Login is already used for other User. Please change Login.");
				System.out.println("@@@ Same Login is already used for other User. Please change Login.");
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return resultObj;
	}
	public ValueObject getData( ValueObject inValObj){
		ValueObject resultObj=new ValueObject();
		User _user=null;
		try{
			long userId=Long.parseLong(inValObj.get("selectedData").toString().trim());
			System.out.println("@@@@@@@@@@@@ userId "+userId);
			_user=UserDao.getInstance().findByPrimaryKey(userId);
		}catch (Exception e) {
			e.printStackTrace();
		}
		resultObj.put("selectedDataObj", _user);
		return resultObj;
	}
	public ValueObject getComboData(ValueObject inValObj){
		ValueObject resultObj=new ValueObject();
		/*State[] state=null;
		City[] city=null;
		StudioMaster[] studios=null;
		User[] _user=null;
		long studioId=-1;
		boolean isSuperAdmin=false;
		try{

			if(inValObj.get("isSuperAdmin")!=null){
				isSuperAdmin=Boolean.parseBoolean(inValObj.get("isSuperAdmin").toString().trim());
			}
			if(isSuperAdmin){
				if(inValObj.get("studioId")!=null){
					studioId=Long.parseLong(inValObj.get("studioId").toString().trim());
				}
			}else{
				User user=(User)inValObj.get("user");
				studioId=user.getStudioId();
			}

			System.out.println("@@@@@@@@@@@@ studioId "+studioId);
			state=StateDao.getInstance().findAll();
			city=CityDao.getInstance().findAll();
			studios=StudioMasterDao.getInstance().findAll();
			_user=UserDao.getInstance().findAllUsersForStudio(studioId);
		}catch (Exception e) {
			e.printStackTrace();
		}
		resultObj.put("selectedStudioId", studioId);
		resultObj.put("stateCombo", state);
		resultObj.put("citiesCombo", city);
		resultObj.put("studiosCombo", studios);
		resultObj.put("comboObject", _user);*/

		return resultObj;

	}

	public ValueObject getSelectedData(ValueObject inValObj) {
		return null;
	}

	public ValueObject saveData(ValueObject inValObj) {
		return null;
	}
	public ValueObject editProfile( ValueObject inValObj){
		ValueObject resultObj=new ValueObject();
		try{

			User _user=(User)inValObj.get("user");

			_user.setName(inValObj.get("name").toString().trim());
			_user.setPassword(inValObj.get("password").toString().trim());
			_user.setMobileNo(inValObj.get("mobile").toString().trim());
			_user.setEmailId(inValObj.get("emailId").toString().trim());

			UserDao.getInstance().update(_user.getUserId(), _user);
			resultObj.put("updatedUser",_user);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return resultObj;
	}
}
